package com.grass.appointment.utils;

import com.androidx.p013lv.base.BaseSdk;
import com.grass.appointment.bean.CutBean;
import com.lzy.okgo.cookie.SerializableCookie;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class FileListUtils {
    public static String getFileMD5s(File file, int i) {
        if (!file.isFile()) {
            return null;
        }
        byte[] bArr = new byte[1024];
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            FileInputStream fileInputStream = new FileInputStream(file);
            while (true) {
                int read = fileInputStream.read(bArr, 0, 1024);
                if (read != -1) {
                    instance.update(bArr, 0, read);
                } else {
                    fileInputStream.close();
                    return new BigInteger(1, instance.digest()).toString(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static List<CutBean> fileList(String str, File file) throws Exception {
        ArrayList arrayList = new ArrayList();
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(new File(BaseSdk.getContext().getExternalFilesDir(null).getAbsolutePath() + "/config.properties"));
        byte[] bArr = new byte[2097152];
        Properties properties = new Properties();
        int i = 0;
        while (true) {
            int read = fileInputStream.read(bArr, 0, 2097152);
            if (read != -1) {
                String str2 = BaseSdk.getContext().getExternalFilesDir(null).getAbsolutePath() + "/" + str + "_" + i;
                StringBuilder sb = new StringBuilder();
                int i2 = i + 1;
                sb.append(i);
                sb.append("");
                properties.setProperty(sb.toString(), str2);
                FileOutputStream fileOutputStream2 = new FileOutputStream(new File(str2));
                fileOutputStream2.write(bArr, 0, read);
                fileOutputStream2.close();
                arrayList.add(new CutBean(i2, str2));
                i = i2;
            } else {
                properties.setProperty(SerializableCookie.NAME, file.getName());
                properties.store(fileOutputStream, "ConfigFile");
                fileInputStream.close();
                return arrayList;
            }
        }
    }

    public static String getMD5(String str) throws Exception {
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(str.getBytes());
            String bigInteger = new BigInteger(1, instance.digest()).toString(16);
            if (bigInteger.length() >= 32) {
                return bigInteger;
            }
            return 0 + bigInteger;
        } catch (Exception unused) {
            throw new Exception("MD5加密出現錯誤");
        }
    }
}
